/* 
 * Copyright 2014 Igor Maznitsa (http://www.igormaznitsa.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.igormaznitsa.jhexed.swing.editor.ui.frames;

import com.igormaznitsa.jhexed.swing.editor.model.*;
import java.awt.*;
import javax.swing.*;

public class FrameToolOptions extends javax.swing.JInternalFrame implements InsideApplicationBus.AppBusListener {
  private static final long serialVersionUID = -1997778093948284499L;
  private final JPanel NO_SELECTED_TOOL;
  
  public FrameToolOptions() {
    initComponents();
    
    NO_SELECTED_TOOL = new JPanel(new BorderLayout());
    NO_SELECTED_TOOL.setMinimumSize(new Dimension(200, 150));
    NO_SELECTED_TOOL.setPreferredSize(new Dimension(200, 150));
    
    final JLabel lbl = new JLabel("Select a tool");
    lbl.setHorizontalAlignment(JLabel.CENTER);
    lbl.setEnabled(false);
    NO_SELECTED_TOOL.add(lbl,BorderLayout.CENTER);

    InsideApplicationBus.getInstance().addAppBusListener(this);
    this.setContentPane(NO_SELECTED_TOOL);
    
    pack();
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    setClosable(true);
    setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
    setResizable(true);
    setTitle("Tool options");
    addComponentListener(new java.awt.event.ComponentAdapter() {
      public void componentHidden(java.awt.event.ComponentEvent evt) {
        formComponentHidden(evt);
      }
      public void componentShown(java.awt.event.ComponentEvent evt) {
        formComponentShown(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 287, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 228, Short.MAX_VALUE)
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void formComponentHidden(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentHidden
    InsideApplicationBus.getInstance().fireEvent(this, InsideApplicationBus.AppBusEvent.A_FRAME_CHANGED_ITS_STATUS, this, FrameType.TOOL_OPTIONS);
  }//GEN-LAST:event_formComponentHidden

  private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
    InsideApplicationBus.getInstance().fireEvent(this, InsideApplicationBus.AppBusEvent.A_FRAME_CHANGED_ITS_STATUS, this, FrameType.TOOL_OPTIONS);
  }//GEN-LAST:event_formComponentShown

  // Variables declaration - do not modify//GEN-BEGIN:variables
  // End of variables declaration//GEN-END:variables

  @Override
  public void setVisible(final boolean flag) {
    super.setVisible(flag);
    if (flag) {
      InsideApplicationBus.getInstance().fireEvent(this, InsideApplicationBus.AppBusEvent.REQUEST_EVENT, InsideApplicationBus.AppBusEvent.SELECTED_LAYER_CHANGED);
    }
  }
  
  @Override
  public void onAppBusEvent(Object source, InsideApplicationBus bus, InsideApplicationBus.AppBusEvent event, Object... objects) {
    if (event == InsideApplicationBus.AppBusEvent.SELECTED_TOOL_CHANGED){
      final ToolType selectedTool = (ToolType)objects[0];
      if (selectedTool == null) {
        this.setContentPane(NO_SELECTED_TOOL);
      }
      else {
        this.setContentPane(selectedTool.getOptions());
      }
      pack();
      revalidate();
      repaint();
    }
  }

  

}
